<?php
/**
 * 发送文本消息
 */
namespace App\Service;

use App\Common;
use App\Model\Callback;
use App\Model\Msg;
use App\Model\UserBind;
use App\ServiceInterface;
use Hyperf\DbConnection\Db;

class SendtextService implements ServiceInterface
{
    private $_srever;

    private $_fd;

    private $_data;

    /**
     * BindService constructor.
     * @param null $server
     * @param int $fd
     * @param array $data
     */
    public function __construct($server=null,int $fd=0,array $data=[])
    {
        $this->_srever = $server;
        $this->_fd     = $fd;
        $this->_data   = $data;
    }

    /**
     * 参数校验
     * @return string
     */
    public function check():string
    {
        if (!isset($this->_data['account']) || $this->_data['account'] == 0)
        {
            $this->_data['account'] = 0;
        }

        if (!isset($this->_data['to_account']) || !$this->_data['to_account'])
        {
            return 'to_account is null';
        }
        return '';
    }

    /**
     * 具体逻辑
     * @return array
     */
    public function init():array
    {
        $time = time();
        $status = 'off-line';
        try {
            $check_msg = $this->check();
            if ($check_msg)
                throw new \Exception($check_msg);

            $user = UserBind::where(
                [
                    'account'=> $this->_data['to_account'],
                    'appid'  => $this->_data['appid'],
                ]
            )->first(['fd','username','avatar']);

            // 判断是否在线
            if ($user && $user['fd'] > 0){
                $status = 'on-line';
                $this->_data['action']   = 'receiveMsg';
                $this->_data['time']     = $time;
                $this->_data['username'] = $user['username'];
                $this->_data['avatar']   = $user['avatar'];

                $this->_srever->push($user['fd'], json_encode($this->_data,JSON_UNESCAPED_UNICODE));
            }

            // 写库
            $msg = [
                'appid'      => $this->_data['appid'],
                'type'       => 1,
                'account'    => $this->_data['account'],
                'to_account' => $this->_data['to_account'],
                'content'    => serialize($this->_data['content']),
                'time'       => $time,
            ];
            $id = Msg::insertGetId($msg);

            $this->callback();

            return [0,'ok',['msgid'=>time().$id, 'status'=>$status]];
        }catch (\Exception $e){
            return [-1,$e->getMessage(),$this->_data];
        }
    }

    /**
     * 发送回调
     */
    private function callback()
    {
        $appid = $this->_data['appid'];
        $callback = Callback::where(['appid'=>$appid])->first(['sendmsg']);
        if (!$callback) return;

        if ($callback['sendmsg']){
            $data = [
                'code' => 0,
                'msg'  => 'ok',
                'info' => [
                    'account' => $this->_data['account'],
                    'to_account' => $this->_data['to_account'],
                    'content' => $this->_data['content'],
                ]
            ];
            Common::http_request($callback['sendmsg'], $data);
        }
    }

}